package com.springBoot.springBootSysCore.modules.services.system.impl;

import com.springBoot.springBootSysCore.common.service.IBaseServiceImpl;
import com.springBoot.springBootSysCore.common.utils.CacheUtils;
import com.springBoot.springBootSysCore.common.utils.returnUtil.CodeEnum;
import com.springBoot.springBootSysCore.common.utils.returnUtil.ResultJsonFormat;
import com.springBoot.springBootSysCore.modules.entity.system.SysCode;
import com.springBoot.springBootSysCore.modules.entity.system.SysDict;
import com.springBoot.springBootSysCore.modules.entity.system.SysRole;
import com.springBoot.springBootSysCore.modules.entity.system.SysUser;
import com.springBoot.springBootSysCore.modules.entity.utils.DictUtils;
import com.springBoot.springBootSysCore.modules.repository.system.SysCodeRepository;
import com.springBoot.springBootSysCore.modules.services.system.SysCodeService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class SysCodeServiceImpl extends IBaseServiceImpl<SysCode,String> implements SysCodeService{
	@Autowired
	private SysCodeRepository sysCodeRepository;


	@Override
	@Transactional
	public Map<String, Object> deleteEntity(SysCode entity) {
		ResultJsonFormat rj = new  ResultJsonFormat("删除成功", CodeEnum.SUCCESS.value());
		try {
			if(StringUtils.isNoneBlank(entity.getId())){
				if(entity.isSureDel()){
//					sysUserRepository.deleteById(user.getId());
				}else{
//					sysUserRepository.updateEntityById(user);
				}
			}else{
				List<String> whereList = new ArrayList<String>();
				String ids = "";
				String[] a = entity.getIds().split(",");
				for(int i = 0 ; i < a.length ;i++ ){
					ids += "'"+a[i]+"'";
					if(i!=a.length-1){
						ids +=",";
					}
				}
				if(entity.isSureDel()){
					sysCodeRepository.deleteByIds(Arrays.asList(a));
				}else{
					sysCodeRepository.updateDelFlag(Arrays.asList(a));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return rj.convertResultJson();
	}

	@Override
	public Map<String, Object> queryVideoLive() {
		ResultJsonFormat rj = new  ResultJsonFormat( CodeEnum.SUCCESS.value());
		try {
			List<SysCode> li = sysCodeRepository.findAll(new Specification<SysCode>() {
				@Override
				public Predicate toPredicate(Root<SysCode> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
					List<Predicate> list = new ArrayList<Predicate>();
					Path<String> typeP = root.get("type");
					list.add(cb.equal(typeP.as(String.class), "4"));
					query.where(list.toArray(new Predicate[list.size()]));
					return query.getRestriction();
				}
			});

			List<String> sli = new ArrayList<>();
			for(SysCode c : li){
				if(c.getCodeValue().equals("1")){
					sli.add(c.getCodeName());
				}
			}
			rj.setData(sli);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return rj.convertResultJson();
	}

	@Override
	public Map<String, Object> queryForUs() {
		ResultJsonFormat rj = new  ResultJsonFormat( CodeEnum.SUCCESS.value());
		try {
			String value = sysCodeRepository.findOne(new Specification<SysCode>() {
				@Override
				public Predicate toPredicate(Root<SysCode> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
					List<Predicate> list = new ArrayList<Predicate>();
					Path<String> typeP = root.get("type");
					Path<String> codeNameP = root.get("codeName");
					Path<String> codeValueP = root.get("codeValue");
					list.add(cb.equal(typeP.as(String.class), "5"));
					list.add(cb.equal(codeNameP.as(String.class), "服务条款"));
					query.where(list.toArray(new Predicate[list.size()]));
					return query.getRestriction();
				}
			}).getCodeValue();
			String value2 = sysCodeRepository.findOne(new Specification<SysCode>() {
				@Override
				public Predicate toPredicate(Root<SysCode> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
					List<Predicate> list = new ArrayList<Predicate>();
					Path<String> typeP = root.get("type");
					Path<String> codeNameP = root.get("codeName");
					Path<String> codeValueP = root.get("codeValue");
					list.add(cb.equal(typeP.as(String.class), "5"));
					list.add(cb.equal(codeNameP.as(String.class), "用户须知"));
					query.where(list.toArray(new Predicate[list.size()]));
					return query.getRestriction();
				}
			}).getCodeValue();
			Map m = new HashedMap();
			m.put("value1",value);
			m.put("value2",value2);
			rj.setData(m);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return rj.convertResultJson();
	}

	@Override
	public Map<String, Object> getCode(final SysCode sysCode) {
		ResultJsonFormat rj = new  ResultJsonFormat( CodeEnum.SUCCESS.value());
		try {
			rj.setData(sysCodeRepository.findOne(new Specification<SysCode>() {
				@Override
				public Predicate toPredicate(Root<SysCode> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
					List<Predicate> list = new ArrayList<Predicate>();
					Path<String> typeP = root.get("type");
					Path<String> codeNameP = root.get("codeName");
					Path<String> codeValueP = root.get("codeValue");
					if (StringUtils.isNotBlank(sysCode.getType())) {
						Predicate P_type = cb.equal(typeP.as(String.class), sysCode.getType());
						list.add(P_type);
					}
					if (StringUtils.isNotBlank(sysCode.getCodeName())) {
						Predicate P_codeName = cb.equal(codeNameP.as(String.class), sysCode.getCodeName());
						list.add(P_codeName);
					}
					if (StringUtils.isNotBlank(sysCode.getCodeValue())) {
						Predicate P_codeValue = cb.equal(codeValueP.as(String.class), sysCode.getCodeValue());
						list.add(P_codeValue);
					}
					query.where(list.toArray(new Predicate[list.size()]));
					return query.getRestriction();
				}
			}));
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return rj.convertResultJson();
	}

	@Override
	public Map<String, Object> getCode(final String type,final String codeName,final String codeValue) {
		ResultJsonFormat rj = new  ResultJsonFormat( CodeEnum.SUCCESS.value());
		try {
			rj.setData(sysCodeRepository.findOne(new Specification<SysCode>() {
				@Override
				public Predicate toPredicate(Root<SysCode> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
					List<Predicate> list = new ArrayList<Predicate>();
					Path<String> typeP = root.get("type");
					Path<String> codeNameP = root.get("codeName");
					Path<String> codeValueP = root.get("codeValue");
					if (StringUtils.isNotBlank(type)) {
						Predicate P_type = cb.equal(typeP.as(String.class), type);
						list.add(P_type);
					}
					if (StringUtils.isNotBlank(codeName)) {
						Predicate P_codeName = cb.equal(codeNameP.as(String.class), codeName);
						list.add(P_codeName);
					}
					if (StringUtils.isNotBlank(codeValue)) {
						Predicate P_codeValue = cb.equal(codeValueP.as(String.class), codeValue);
						list.add(P_codeValue);
					}
					query.where(list.toArray(new Predicate[list.size()]));
					return query.getRestriction();
				}
			}));
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return rj.convertResultJson();
	}
	@Override
	@Transactional
	public Map<String, Object> save(SysCode sysDict) {
		ResultJsonFormat rj = new  ResultJsonFormat("保存成功", CodeEnum.SUCCESS.value());
		try {
			//此处检查参数是否规范
			if(StringUtils.isBlank(sysDict.getId())){
				rj.isAdd();
				sysDict.preInsert();
				sysDict.setIsDel("0");
				sysDict.setIsOnly("1");
				super.saveEntiey(sysDict);
			}else{
				SysCode dbcode = sysCodeRepository.findOne(sysDict.getId());
				dbcode.preUpdate();
				dbcode.EntityCopy(dbcode,sysDict);
//				super.saveEntiey(sysDict);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return rj.convertResultJson();
	}


}
